<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="HAdd('0',1)">添加权限</el-button>
        </div>
        <el-table row-key="id" border :data="list">
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template #default="{row}">
              <!-- 按钮级别 不用添加按钮 -->
              <el-button
                v-if="row.type === 1"
                type="text"
                @click="HAdd(row.id,2)"
              >添加</el-button>
              <el-button type="text" @click="doIsEdit(row)">编辑</el-button>
              <el-button type="text" @click="hDel(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
      <!-- 弹层区 -->
      <el-dialog ref="form" :visible.sync="showDialog" title="弹层标题" @close="resetForm">
        <!-- 表单内容 -->
        <el-form label-width="100px" :rules="rules">
          <el-form-item label="权限名称" prop="name">
            <el-input v-model="formData.name" />
          </el-form-item>
          <el-form-item label="权限标识" prop="code">
            <el-input v-model="formData.code" />
          </el-form-item>
          <el-form-item label="权限描述" prop="description">
            <el-input v-model="formData.description" />
          </el-form-item>
          <el-form-item label="权限启用">
            <el-switch
              v-model="formData.enVisible"
              active-text="启用"
              active-value="1"
              inactive-text="不启用"
              inactive-value="0"
              prop="enVisible"
            />
          </el-form-item>
        </el-form>

        <template #footer>
          <div style="text-align: right;">
            <el-button @click="showDialog = false">取消</el-button>
            <el-button type="primary" @click="hisEdit">确定</el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { flatToTree } from '@/utils/index'
import { getPermissionList, addPermission, delPermission, updatePermission } from '@/api/permisson'
export default {
  data() {
    return {
      list: [],
      showDialog: false,
      formData: {
        name: '',
        code: '',
        description: '',
        enVisible: '',
        pid: '',
        type: 1
      },
      rules: {
        name: '',
        code: [],
        description: [],
        enVisible: []
      },
      isEdit: false
    }
  },
  created() {
    this.loadPermissionList()
  },
  methods: {
    // 判断是否是编辑还是添加 在确定那里触发
    hisEdit() {
      this.isEdit ? this.loadUpdatePermission() : this.loadPermission()
    },
    // 启动编辑
    doIsEdit(val) {
      this.isEdit = true
      this.showDialog = true
      this.formData = val
    },
    // 更改权限
    async loadUpdatePermission() {
      const res = await updatePermission(this.formData)
      console.log(res)
      this.showDialog = false
      this.loadPermissionList()
    },
    async hDel(id) {
      try {
        const res = await delPermission(id)
        this.$message.success(res.message)
        this.loadPermissionList()
      } catch (error) {
        console.log(error)
      }
    },
    resetForm() {
      // 清空数据 本来是用 resetFiedls 但是 不起作用
      this.formData = {
        name: '',
        code: '',
        description: '',
        enVisible: '',
        pid: '',
        type: 1
      }
    },
    // 添加权限
    async loadPermission() {
      try {
        const res = await addPermission(this.formData)
        console.log('addPerm新增权限ok 此处是permissions新增权限', res)
        // console.log('新增权限ok 此处是permissions新增权限')
        // 保存数据
        this.loadPermissionList()
        this.showDialog = false
      } catch (err) {
        console.log('addPermission', err)
      }
    },
    HAdd(pid, type) {
      this.formData.pid = pid
      this.formData.type = type
      this.showDialog = true
    },
    async loadPermissionList() {
      try {
        const res = await getPermissionList()
        console.log('getPermissionList获取权限站点', res)
        // 保存数据
        this.list = flatToTree(res.data)
      } catch (err) {
        console.log('getPermissionList', err)
      }
    }
  }
}

</script>
